tempspritespace = appbackupscreen
;--------------------------------
;Large Clip Sprite
;ix->sprite
;b = height
;c = width in bytes
;d = x
;e = y
LargeClippedSprite:
;Check if its even on screen
	ld a,e
	cp 64
	ret p
	add a,b
	ret m
	ret z
	ld a,d
	cp 96
	ret p
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	ret m
	ret z

	ld a,e
	or a
	jp p,Check_clip_bottom
	neg
	push de
	ld hl,0
	ld d,l
	ld e,a
	bit 2,c
	jr z,_
	add hl,de
_
	add hl,hl	
	bit 1,c
	jr z,_
	add hl,de
_
	add hl,hl	
	bit 0,c
	jr z,_
	add hl,de
_
	pop de
	ex de,hl
	add ix,de		;Here you can save the top offset
	add iy,de
	ex de,hl
	ld e,0
	neg
	add a,b
	ld b,a
Check_clip_bottom:
	ld a,e
	add a,b
	sub 64
	jp m,Check_clip_Left
	neg
	add a,b
	ld b,a
Check_clip_Left:
;may want to save b
	xor a
	ld (bigskip),a
	ld (bigskip+1),a
	ld (Do_Clipleft),a
	ld a,d
	or a
	jp p,Check_clip_right
	cpl
	and $F8
	rra
	rra
	rra
	ex de,hl		;save the clipped left offset
	ld e,a
	ld d,0
	add ix,de
	add iy,de
	ld (bigskip),a
	ex de,hl
	inc a
	neg
	add a,c
	ld c,a
	ld a,Clipleftsize 
	ld (Do_Clipleft),a
	ld a,d
	and $07
	ld d,a
Check_clip_right:
	xor a
	ld (Do_Clipright),a
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	sub 96
	jp m,Check_clip_middle
	and $F8
	rra
	rra
	rra
	ld l,a
	ld a,(bigskip)
	add a,l
	inc a
	ld (bigskip),a
	neg
	add a,c
	ld c,a
	ld a,Cliprightsize
	ld (Do_Clipright),a
Check_clip_middle:
;this is where C should be saved.
	ld a,1
	ld (Do_ClipMiddle),a
	ld a,c
	or a
	jp nz,dontskipmiddle
	xor a
	ld (Do_ClipMiddle),a
dontskipmiddle:
	ld l,e
	ld a,d	
	ld h,0
	ld d,h
	add hl,hl
	add hl,de
	add hl,hl
	add hl,hl
	ld e,a
	and $07
	ld (BigRot),a
	push bc
	or a
	ld b,a
	ld a,$ff
	jr z,++_
_
	srl a
	djnz -_
_
	pop bc
	srl e
	srl e
	srl e
	add hl,de
	ld de,plotsscreen
	add hl,de
;this is where plotsscreen offset should be saved
	ld d,a
	cpl
	ld e,a
;masks should be saved to
BigSpriteRow:
	push bc
	push hl
	ld b,c
Do_Clipleft = tempspritespace
	ld a,(Do_ClipLeft)
	or a
	jr z,Clipleft
	push bc
	ld a,(bigrot)
	or a
	ld b,a
	ld a,(ix)
	ld c,(iy)
	inc ix
	inc iy
	jr z,++_
BigRot = tempspritespace+1
_
	rrca
	rrc c
	djnz -_
_
	and e
	ld b,a
	ld a,c
	and e
	or d
	ld c,a
	ld a,(hl)
	and c
	or b
	ld (hl),a
	pop bc
Clipleft:
Clipleftsize = 1
Do_ClipMiddle = tempspritespace+2
	ld a,(Do_ClipMiddle)
	or a
	jr z,ClipMiddle
BigSpriteloop:
	push bc
	ld a,(bigrot)
	or a
	ld b,a
	ld a,(ix)
	ld c,(iy)
	inc ix
	inc iy
	jr z,++_
_
	rrca
	rrc c
	djnz -_
_
	ld b,a
	push bc
		and d
		ld b,a
		ld a,c
		and d
		or e
		ld c,a
		ld a,(hl)
		and c
		or b
		ld (hl),a
		inc hl
	pop bc
	ld a,b
	and e
	ld b,a
	ld a,c
	and e
	or d
	ld c,a
	ld a,(hl)
	and c
	or b
	ld (hl),a
	pop bc
	djnz BigSpriteloop
ClipMiddle:
ClipMiddlesize = 1
Do_ClipRight = tempspritespace+3
	ld a,(Do_ClipRight)
	or a
	jr z,ClipRight
	push bc
	ld a,(bigrot)
	or a
	ld b,a
	ld a,(ix)
	ld c,(iy)
	jr z,++_
_
	rrca
	rrc c
	djnz -_
_
	and d
	ld b,a
	ld a,c
	and d
	or e
	ld c,a
	ld a,(hl)
	and c
	or b
	ld (hl),a
	pop bc
ClipRight:
Cliprightsize = 1
	pop hl
	ld bc,12			;width of the screen
	add hl,bc
bigskip = tempspritespace+4
	ld bc,(bigskip)
	add ix,bc
	add iy,bc
	pop bc
	dec b
	jp nz,bigspriterow
	ret









_Rectangle_:
 	bit    2,a
       res    2,a
       ld     (LineColor),a
       jr     nz,RectFilled
       push   de
       push   bc
       ld     h,b
       ld     l,e
       call   DoLine        ;top,left to top right
       pop    hl
       pop    de
       push   hl
       push   de
       ld     h,d           ;top,left to bottom,left
       inc    e
       dec    l
       call   DoLine
       pop    de
       pop    hl
       push   de
       push   hl
       ld     d,h           ;top,right to bottom,right
       inc    e
       dec    l
       call   DoLine
       pop    hl
       pop    de
       ld     e,l           ;bottom,left to bottom,right
       jp     DoLine
RectFilled:
       ld     h,b
       ld     l,e
       push   de
       push   bc
       call   DoLine
       pop    bc
       pop    de
       inc    e
       ld     a,c
       cp     e
       jr     nc,RectFilled
       ret







;------------------------------------------------------------
;-------------------DRAWING LINES----------------------------
;------------------------------------------------------------

_Line_:

DoLine:       
	ld	a,1
	ld	(xdir),a
	ld	(ydir),a
	ld	a,d
	sub	h
	ld	b,a
	jp	nc,flokx
	neg
	ld	b,a
	ld	a,255
	ld	(xdir),a
flokx:
	ld	a,e
	sub	l
	ld	c,a
	jp	nc,floky
	neg
	ld	c,a
	ld	a,255
	ld	(ydir),a	
floky:
	ld	e,0	;error
	ld	a,b
	cp	c
	jp	c,fly
	ld	d,b	;resx
	inc	b
fllx:
	call	pixelonfl
	ld	a,(xdir)
	add	a,h
	ld	h,a
	ld	a,e
	add	a,c
	ld	e,a
	cp	d
	jp	c,flxok
	sub	d
	ld	e,a
	ld	a,(ydir)
	add	a,l
	ld	l,a
flxok:
	djnz	fllx
	ret
fly:
	ld	a,b
	ld	b,c
	ld	c,a
	ld	d,b
	inc	b
flly:
	call	pixelonfl
	ld	a,(ydir)
	add	a,l
	ld	l,a
	ld	a,e
	add	a,c
	ld	e,a
	cp	d
	jp	c,flyok
	sub	d
	ld	e,a
	ld	a,(xdir)
	add	a,h
	ld	h,a
flyok:
	djnz	flly
	ret

pixelonfl:
       push   hl
       push   de
       push   bc
       ex     de,hl
       ld     a,(LineColor)
       call   PointLineCont
       pop    bc
       pop    de
       pop    hl
       ret

PointLineCont:
       ld     c,a
       ld     a,d
       push   de
       inc    c
       dec    c
       jr     z,PointOff
       dec    c
       jr     z,PointOn

	call	GetPixel
	xor	(hl)
PointCont:
       ld     (hl),a
       pop    de                   ;D = X, E = Y
       push   af
       ld     a,d
       srl    a
       srl    a
       srl    a
       add    a,20h

       ld     a,80h
       add    a,e

       pop    af

       ret
PointOff:
       call   GetPixel
       cpl
       and    (hl)
       jr     PointCont
PointOn:
       call   GetPixel
       or     (hl)
       jr     PointCont

GetPixel:
                            ;A = X coord
                            ;E = y coord
       ld     d,0
       ld     h,d
       ld     l,e
       add    hl,de
       add    hl,de
       add    hl,hl
       add    hl,hl
       ld     de,plotSScreen
       add    hl,de
       ld     b,0
       ld     c,a
       and    7
       srl    c
       srl    c
       srl    c
       add    hl,bc
       ld     b,a
       inc    b
       ld     a,1
GetPixelLoop:
       rrca
       djnz   GetPixelLoop
	ret
